<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/team.class.php");

$team = new team;
$team->status=STATUS_ACTIVE;

$list = $loader->loadListBy($team,$team->listByStatus());

$tm->assign('timezonelist',$TIMEZONE_LIST);
$tm->assign('langlist',$LANGUAGE_LIST);
$tm->assign('teamlist',$list);
$tm->assign('current',$CURRENT_USER);

if ($param['op']=='submit' and $flow_ok) {
	if (isEmpty($param['mail'])
		or
		isEmpty($param['lang'])
		or
		(isEmpty($param['team']) AND $CURRENT_USER->groupid != GROUP_ADMIN)
		or
		isEmpty($param['timezone'])
		) {
		$error = $LANG['regmissing'];
		$tm->assign('error',$error);		
		$content = $tm->fetch('userpref.tpl');
	} elseif (!isValidEmail($param['mail'])) {
		$error = $LANG['invalidmail'];
		$tm->assign('error',$error);		
		$content = $tm->fetch('userpref.tpl');
	} else {
		$us = new user;
		$us->userid=$CURRENT_USER->userid;
		$ret = $loader->get($us);
		if ($ret) {
			if ($CURRENT_USER->groupid != GROUP_ADMIN) {
				if ($us->teamid!=$param['team']) {
					$us->status=STATUS_INACTIVE;
					$team = new team;
					$team->id=$us->teamid;
					$loader->get($team);
					$oldteam=$team;
				}
				$us->teamid=$param['team'];	
			}

			$us->mail=$param['mail'];
			$us->lang=$param['lang'];
			$us->timezone=$param['timezone'];
			$us->setAllowUserEmail($param['allowmp']);
			$us->setNotifyOnChange($param['mponchange']);
			$us->setNotifyTask($param['mptask']);
			
			$ret = $loader->update($us);
			
			updateLang($us->lang);
			$tm->assign('lang',$LANG);
	
			if ($us->status==STATUS_INACTIVE) {
				sendTeamMail($us,$oldteam);
				sendUserStatusMail($us);
				include("includes/logout.php");
			}
			$message = $LANG['userupdated'];
			$content = createMessage($homelink,array($LANG['home']));
		}
	}
} else {
	$content = $tm->fetch('userpref.tpl');
}

?>
